<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\BoxMemberInviteLog;
use app\admin\model\Order;
use app\common\controller\Export;
use app\qsapi\model\Member;

/**
 * Class BoxInviteService
 * @package app\admin\service
 */
class BoxInviteService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxMemberInviteLog();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2023/09/08
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];

        //邀请人昵称
        if (isset($param['invite_member_nickname']) && !empty($param['invite_member_nickname'])) {
            $map[] = ['m1.nickname', '=', $param['invite_member_nickname']];
        }

        // 邀请人编号
        if (isset($param['invite_member_id']) && !empty($param['invite_member_id'])) {
            $map[] = ['l.invite_member_id', '=', $param['invite_member_id']];
        }

        // 创建时间
        if (isset($param['create_time']) && !empty($param['create_time'])) {
            $map[] = ['l.create_time', '>=', $param['create_time'][0]];
            $map[] = ['l.create_time', '<=', $param['create_time'][1]];
        }

        $map[] = ['l.is_valid', '=', 1];

        $list = $this->model->alias('l')
            ->field('l.*,m1.nickname as invite_member_nickname,m2.nickname as member_nickname,m2.phone,m2.create_time as register_time')
            ->leftJoin('cq_member m1', 'm1.id = l.invite_member_id')
            ->leftJoin('cq_member m2', 'm2.id = l.member_id')
            ->where($map)
            ->order('l.id desc')
            ->paginate($param['limit']);
        if (empty($list)) {
            return message('数据为空', false);
        }
        $list = $list->toArray();
        foreach ($list['data'] as &$item) {
            $item['phone']         = !empty($item['phone']) ? substr_replace($item['phone'], '****', 3, 4) : '未绑定';
            $memberIds             = (new Member())->where(['pid' => $item['member_id'], 'status' => 1])->column('id');
            $item['amountTotal']   = (new Order())->whereIn('member_id', $memberIds)->where(['is_box' => 1, 'pay_status' => 1, 'mark' => 1, 'is_sandbox' => 0])->sum('pay_amount');
            $item['register_time'] = date('Y-m-d H:i:s', $item['register_time']);
        }
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list['data'],
            "count" => $list['total'],
        ];
    }

    /**
     * @Notes:邀请有奖数据导出
     * @Interface export
     * @return array
     */
    public function export()
    {
        $xlsName = "邀请有奖数据统计";
        $xlsCell = array(
            array('id', 'ID'),
            array('invite_member_nickname', '邀请人昵称'),
            array('invite_member_id', '邀请人ID'),
            array('invite_score', '邀请人所获积分'),
            array('member_nickname', '被邀请人昵称'),
            array('member_id', '被邀请人ID'),
            array('phone', '被邀请人手机号'),
            array('register_time', '注册时间'),
            array('amountTotal', '被邀请人充值金额'),
        );
        $param   = request()->param();

        // 查询条件
        $map = [];

        //邀请人昵称
        if (isset($param['invite_member_nickname']) && !empty($param['invite_member_nickname'])) {
            $map[] = ['m1.nickname', '=', $param['invite_member_nickname']];
        }

        // 邀请人编号
        if (isset($param['invite_member_id']) && !empty($param['invite_member_id'])) {
            $map[] = ['l.invite_member_id', '=', $param['invite_member_id']];
        }

        // 创建时间
        if (isset($param['create_time']) && !empty($param['create_time'])) {
            $map[] = ['l.create_time', '>=', $param['create_time'][0]];
            $map[] = ['l.create_time', '<=', $param['create_time'][1]];
        }

        $map[] = ['l.is_valid', '=', 1];

        $list = $this->model->alias('l')
            ->field('l.*,m1.nickname as invite_member_nickname,m2.nickname as member_nickname,m2.phone,m2.create_time as register_time')
            ->leftJoin('cq_member m1', 'm1.id = l.invite_member_id')
            ->leftJoin('cq_member m2', 'm2.id = l.member_id')
            ->where($map)
            ->order('l.id desc')
            ->select();
        foreach ($list as &$item) {
            $item['phone']         = !empty($item['phone']) ? substr_replace($item['phone'], '****', 3, 4) : '未绑定';
            $memberIds             = (new Member())->where(['pid' => $item['member_id'], 'status' => 1])->column('id');
            $item['amountTotal']   = (new Order())->whereIn('member_id', $memberIds)->where(['is_box' => 1, 'pay_status' => 1, 'mark' => 1, 'is_sandbox' => 0])->sum('pay_amount');
            $item['register_time'] = date('Y-m-d H:i:s', $item['register_time']);
        }

        $Export = new Export();
        $Export->exportExcel($xlsName, $xlsCell, $list);
    }

}
